import * as Remote from '../services';

const WindowStateModel = {
    namespace: 'windowstate',
    state: {
        windowState:'normal'
    },
    effects: {
        *getWindowState({ payload }, { call, put }) {
            let ret = yield call(Remote.getWindowState);
            yield put({
                type:'init',
                payload:{
                    windowState:ret.windowState
                }
            })
        },
        *quit({ payload }, { call, put }) {
            let ret = yield call(Remote.quit);

        },
        *maximize({ payload }, { call, put }) {
            let ret = yield call(Remote.maximize);   
            yield put({
                type:'init',
                payload:{
                    windowState:ret.windowState
                }
            })        
        },
        *minimize({ payload }, { call, put }) {
            let ret = yield call(Remote.minimize);  
            yield put({
                type:'init',
                payload:{
                    windowState:ret.windowState
                }
            })          
        },
    },
    reducers: {
        init(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/') {
                    dispatch({
                        type: 'getWindowState',
                    })
                }
            });
        }
    }
};

export default WindowStateModel;